package gameobjects
{
	import assets.Globals;
	import assets.Images;
	
	import com.greensock.TweenMax;
	import com.greensock.easing.Linear;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import logic.Score;
	
	import managers.SoundManager;

	public class Enemy extends GameObject
	{
		protected var projectile:Projectile = null;
		protected var attackRate:Number = 0;
		protected var myTween:TweenMax = null;
		protected var bar:Bar;
		protected var shadow:Sprite = null;
		
		public function Enemy(speed:Number = 40, attackRate:Number = 0.5, health:int = 100)
		{
			shadow = Globals.shadow(20, 40);
			addChild(shadow);
			super(health);
			this.attackRate = attackRate;
			this.speed = speed;
			this.bar = new Bar(this,15,45);
			Globals.enemies.push(this);
			myTween = new TweenMax(this, 1000/speed, { x:-100, ease:Linear.easeNone });
			attackTimer = new Timer(1000/attackRate);
			attackTimer.addEventListener(TimerEvent.TIMER, timerHandler);
	
		}
		
		override public function destroy():void
		{			
//			if (Score.getEnemiesKilled() > 5) Globals.menu.show(2);
			
			myTween.kill();
			Globals.enemies.splice(Globals.enemies.indexOf(this), 1);
			if (bar.parent == this)
				removeChild(bar);
			
			var destructionTimer:Timer = new Timer(4000, 1);
			destructionTimer.addEventListener("timer", destruction);
			destructionTimer.start();
		
			attackTimer.removeEventListener(TimerEvent.TIMER, timerHandler);
			attackTimer.stop();
			SoundManager.monsterDead();
		}
		
		private function destruction(event:TimerEvent):void
		{
			TweenMax.to(this, 1, { alpha:0, y: this.y-40, onComplete:finalDestruction });
			TweenMax.to(shadow, 1, { alpha:0, y: shadow.y+40 });
		}
		
		private function finalDestruction():void
		{
			super.destroy();
		}
		
		private var attackTimer:Timer;
		
		public function stop():void 
		{
			myTween.pause();
			attackTimer.stop();
			attacking = false;
		}
		
		public function pause():void
		{
			attackTimer.stop()
		}
		
		public function resume():void
		{
			if (attacking)
				attackTimer.start()
		}
		
		public function move():void
		{
			myTween.resume();
			attackTimer.stop();
			attacking = false;
		}
		
		private var attacking:Boolean = false;
		
		public function attack():void
		{
			myTween.pause();
			attackTimer.start();
			attacking = true;
			SoundManager.monsterFight();
		}
		
		private var shifting:Boolean = false;
		
		public function shift():void
		{
			var sign:int = 0;
			if (fieldY == 0) sign = 1;
			else if (fieldY == Globals.TILES_Y-1) sign = -1;
			else sign = Math.random() < .5 ? -1 : 1;
			fieldY += sign;
			TweenMax.to(this, 0.5, { y: y + sign * 68, onComplete: resetShifting });
			shifting = true;
		}
		
		private function resetShifting():void
		{
			shifting = false;
		}
		
		public function isAttacking():Boolean
		{
			return attacking;
		}
		
		public function isShifting():Boolean
		{
			return shifting;
		}
		
		public function timerHandler(event:TimerEvent):void
		{
			var invisible:Projectile = new InvisibleProjectile(this);
			invisible.x = x+46;
			invisible.y = y + 15;
			Globals.game.addChild(invisible);
		}
		
		override public function decreaseHealth(by:Number):Boolean
		{
			var result:Boolean = super.decreaseHealth(by);
			bar.update();
			if (Math.random() > .85)
				shift();
			return result;
		}
		
		public function getBar():Bar
		{
			return bar;
		}
	}
}